package com.deductive.web.disposition.symbol;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.deductive.common.config.RuoYiConfig;
import com.deductive.common.core.controller.BaseController;
import com.deductive.common.core.domain.AjaxResult;
import com.deductive.common.core.page.TableDataInfo;
import com.deductive.common.utils.file.FileUploadUtils;
import com.deductive.common.utils.file.FileUtils;
import com.deductive.domain.symbol.Symbol;
import com.deductive.framework.config.ServerConfig;
import com.deductive.service.symbol.SymbolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/desdSymbol")
@Api(tags = "专题符号管理")
public class SymbolController extends BaseController {

    @Autowired
    private SymbolService desdSymbolService;



    @Autowired
    private ServerConfig serverConfig;


    @ApiOperation("获取符号(分页)")
    @GetMapping("/getSymbolPage")
    public TableDataInfo getLayerList(Symbol symbol) {
        startPage();
        List<Symbol> symbolList = desdSymbolService.listPage(symbol);
        return getDataTable(symbolList);
    }


    @ApiOperation("获取符号")
    @GetMapping("/getSymbolListAll")
    public AjaxResult getSymbolListAll(Symbol symbol) {
        LambdaQueryWrapper<Symbol> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(symbol.getSymbolName()!=null,Symbol::getSymbolName,symbol.getSymbolName())
                .like(symbol.getSymbolType()!=null,Symbol::getSymbolType,symbol.getSymbolType());
        List<Symbol> symbolList = desdSymbolService.list(wrapper);
        return AjaxResult.success(symbolList);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("根据Id查询结果")
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return AjaxResult.success(this.desdSymbolService.getById(id));
    }


    /**
     * 修改数据
     *
     * @return 修改结果
     */
    @ApiOperation("符号修改")
    @PutMapping
    public AjaxResult update(@RequestBody Symbol symbol) {
        return AjaxResult.success(this.desdSymbolService.updateById(symbol));
    }

    @ApiOperation("添加符号")
    @PostMapping
    public AjaxResult insertLayer(@RequestBody Symbol symbol) {
        LambdaQueryWrapper<Symbol> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(symbol.getSymbolName()!=null,Symbol::getSymbolName,symbol.getSymbolName());
        List<Symbol> symbolList = desdSymbolService.list(wrapper);
        if (symbolList.size()>0){
            return AjaxResult.error("数据库中存在同名图标！");
        }
        return AjaxResult.success(this.desdSymbolService.save(symbol));

    }

    @ApiOperation("删除符号")
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        List<Long> list = new ArrayList<>(Arrays.asList(ids));
        boolean removeByIds = desdSymbolService.removeByIds(list);
        return AjaxResult.success(removeByIds);
    }

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/upload")
    @ApiOperation("上传")
    public AjaxResult uploadFile(@RequestPart("file") MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getSymbol();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
